unit uFormSettings;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, ShellAPI, Graphics, Controls,
  Forms, Registry, Dialogs, StdCtrls, ExtCtrls, Buttons, ComCtrls;

type
  TFormSetting = class(TForm)
    btnSave: TButton;
    btnCancel: TButton;
    pnlAction: TPanel;
    gbxMergeToolSettings: TGroupBox;
    lblLocation: TLabel;
    lblParameters: TLabel;
    edtLocation: TEdit;
    edtParameters: TEdit;
    sbtnFindMergeProgram: TSpeedButton;
    odFindMergeProgram: TOpenDialog;
    pbcContent: TPageControl;
    tbsSettings: TTabSheet;
    tbsAbout: TTabSheet;
    Label1: TLabel;
    cbxConfirm: TCheckBox;
    lblAuthor: TLabel;
    lblSubTitle: TLabel;
    lblProductName: TLabel;
    lblAppVersion: TLabel;
    btnSetDefault: TButton;
    lblHomepage: TLabel;
    procedure lblHomepageClick(Sender: TObject);
    procedure btnSetDefaultClick(Sender: TObject);
    procedure lblAuthorClick(Sender: TObject);
    procedure pbcContentChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure sbtnFindMergeProgramClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormSetting: TFormSetting;

implementation

uses uUtils, uConfiguration;

{$R *.dfm}

procedure TFormSetting.sbtnFindMergeProgramClick(Sender: TObject);
begin
  if odFindMergeProgram.Execute() then
  begin
    edtLocation.Text := odFindMergeProgram.FileName;
  end;
end;

procedure TFormSetting.btnCancelClick(Sender: TObject);
begin
  Self.Close();
end;

procedure TFormSetting.btnSaveClick(Sender: TObject);
var
  configuration: TConfigurations;
begin
  configuration := TConfigurations.Create();
  with configuration do
  begin
    Location := edtLocation.Text;
    Parameters := edtParameters.Text;
    ConfirmFileClean := cbxConfirm.Checked;
  end;

  Self.Close();
end;

procedure TFormSetting.FormCreate(Sender: TObject);
var
  configuration: TConfigurations;
begin
  pbcContent.ActivePage := tbsAbout;
  pbcContentChange(pbcContent);

  configuration := TConfigurations.Create();
  with configuration do
  begin
    edtLocation.Text := Location;
    odFindMergeProgram.FileName := edtLocation.Text;
    edtParameters.Text := Parameters;
    cbxConfirm.Checked := ConfirmFileClean;
  end;

  lblAppVersion.Caption := GetFileVersion(Application.ExeName);
end;

procedure TFormSetting.pbcContentChange(Sender: TObject);
var
  pbcSender: TPageControl;
begin
  pbcSender := Sender as TPageControl;
  if pbcSender.ActivePage = tbsSettings then
  begin
    btnSave.Visible := true;
    btnCancel.Caption := '&Cancel';
  end
  else
  begin
    btnSave.Visible := false;
    btnCancel.Caption := 'OK';
  end;
end;

procedure TFormSetting.lblAuthorClick(Sender: TObject);
begin
  ShellExecute(handle, 'open', 'mailto:netcobra@gmail.com?subject=About TortoiseSVN Merge', nil, nil, SW_SHOWNORMAL);
end;

procedure TFormSetting.lblHomepageClick(Sender: TObject);
begin
  ShellExecute(handle, 'open', 'http://code.google.com/p/tsvnmerge/', nil, nil, SW_SHOWNORMAL);
end;

procedure TFormSetting.btnSetDefaultClick(Sender: TObject);
var
  diffCommandLine: string;
  sysRegistry: TRegistry;
  tips: string;
begin
  sysRegistry := TRegistry.Create();
  diffCommandLine := Application.ExeName + ' %mine %theirs %base %merged';

  with sysRegistry do
  begin
    RootKey := HKEY_CURRENT_USER;
    try
      try
        Openkey('Software\TortoiseSVN', false);
        WriteString('Merge', diffCommandLine);
      except on E: Exception do
        begin
          tips := 'Failed set registry.' + #13#10 + 'Error Message:' +
            E.Message;
          MessageDlg(PAnsiChar(tips), mtInformation, [mbOK], 0);
          Exit;
        end;
      end;
    finally
      CloseKey;
    end;
  end;
end;

end.
